package cc.idoone.spring_boot_hello.upload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("/upload")
public class UploadController2 {

	@RequestMapping(value = "/singlefile", method = RequestMethod.POST)
	@ResponseBody
	public String uploadFile(MultipartHttpServletRequest multiReq) {
		// 获取上传文件的路径
		MultipartFile file = multiReq.getFile("myfile");
		String uploadFilePath = file.getOriginalFilename();
		System.out.println("uploadFlePath: " + uploadFilePath);

		// 截取上传文件的文件名
		String uploadFileName = uploadFilePath.substring(
				uploadFilePath.lastIndexOf('\\') + 1, uploadFilePath.lastIndexOf('.'));
		System.out.println("multiReq.getFile() " + uploadFileName);

		// 截取上传文件的后缀
		String uploadFileSuffix = uploadFilePath.substring(
				uploadFilePath.lastIndexOf('.') + 1, uploadFilePath.length());
		System.out.println("uploadFileSuffix: " + uploadFileSuffix);

		FileOutputStream fos = null;
		InputStream fis = null;
		try {
			fis = file.getInputStream();
			fos = new FileOutputStream(new File("D://storage/p9/" + uploadFileName
					+ ".")
					+ uploadFileSuffix);
			byte[] buf = new byte[1024];
			int i = fis.read(buf);
			while (i != -1) {
				fos.write(buf, 0, buf.length);
				fos.flush();
				i = fis.read(buf);
			}

			return "success";
		} catch (IOException e) {
			e.printStackTrace();
			return "failure";
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@RequestMapping(value = "/multiplefile", method = RequestMethod.POST)
	@ResponseBody
	public String uploadFiles(MultipartHttpServletRequest request) {
		List<MultipartFile> files = request.getFiles("myfile");
		MultipartFile file = null;
		BufferedOutputStream stream = null;
		for (int i = 0; i < files.size(); ++i) {
			file = files.get(i);
			if (!file.isEmpty()) {
				try {
					String uploadFilePath = file.getOriginalFilename();
					System.out.println("uploadFlePath: " + uploadFilePath);
					// 截取上传文件的文件名
					String uploadFileName = uploadFilePath
							.substring(uploadFilePath.lastIndexOf('\\') + 1,
									uploadFilePath.indexOf('.'));
					System.out.println("multiReq.getFile() " + uploadFileName);
					// 截取上传文件的后缀
					String uploadFileSuffix = uploadFilePath.substring(
							uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
					System.out.println("uploadFileSuffix: " + uploadFileSuffix);
					stream = new BufferedOutputStream(new FileOutputStream(new File(
							"D://storage/p9/" + uploadFileName + "." + uploadFileSuffix)));
					byte[] bytes = file.getBytes();
					stream.write(bytes,0,bytes.length);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						if (stream != null) {
							stream.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} else {
				return ("上传文件为空");
			}
		}
		return ("文件接收成功了");
	}
}
